﻿using System;
using System.Data;
using BLL;
using ET;
using Ext.Net;

namespace ERP.Modules
{
    public partial class LockWarehouse : System.Web.UI.UserControl
    {
        readonly LockWarehouse_BLL clsLockWarehouse = new LockWarehouse_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                GetData();
                ClearTextBox();
            }
        }

        private void GetData()
        {
            StoreLockWarehouse.DataSource = clsLockWarehouse.BindGirdtb_LockWarehouse(string.Empty);
            StoreLockWarehouse.DataBind();
        }

        protected void LockWarehouseDblClick(object sender, DirectEventArgs e)
        {
            ClearTextBox();
            int LockWarehouse_Id = int.Parse(e.ExtraParams["LockWarehouse_ID"]);
            DataDetail(LockWarehouse_Id);
            WindowsLockWarehouse.Show();
        }

        private void DataDetail(int lockWarehouseId)
        {
            DataTable dtLockWarehouse = clsLockWarehouse.BindGirdtb_LockWarehouseSelectRow(lockWarehouseId);
            if (dtLockWarehouse != null && dtLockWarehouse.Rows.Count > 0)
            {
                txtLockWarehouse_ID.Text = dtLockWarehouse.Rows[0]["LockWarehouse_ID"].ToString();
                txtLockWarehouse_No.Text = dtLockWarehouse.Rows[0]["LockWarehouse_No"].ToString();
                txtLockWarehouse_Test.Text = dtLockWarehouse.Rows[0]["LockWarehouse_Test"].ToString();
                txtLockWarehouse_Note.Text = dtLockWarehouse.Rows[0]["LockWarehouse_Note"].ToString();
            }

            var clsLockWarehouseDetail = new LockWarehouse_Detail_BLL();
            StoreLockWarehouse_Detail.DataSource = clsLockWarehouseDetail.BindGirdtb_LockWarehouse_Detail(lockWarehouseId);
            StoreLockWarehouse_Detail.DataBind();
        }

        protected void StoreLockWarehouse_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetData();
        }

        protected void StoreLockWarehouse_Detail_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetLockWarehouseDetail();
        }

        protected void Lock_Click(object sender, DirectEventArgs e)
        {
            WindowsLockWarehouse.Show();
            GetLockWarehouseDetail();
            ClearTextBox();
        }

        void GetLockWarehouseDetail()
        {
            var cls = new LockWarehouse_BLL();
            StoreLockWarehouse_Detail.DataSource = cls.BindGirdtb_LockWarehouseDetail();
            StoreLockWarehouse_Detail.DataBind();
        }

        protected void SavePopup_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var GetLockWarehouse_Detail = e.ExtraParams["grLockWarehouse_Detail"];

                var dtLockWarehouse_Detail = JSON.Deserialize<DataTable>(GetLockWarehouse_Detail);

                var obj = new LockWarehouse_ET
                {
                    LockWarehouse_No = txtLockWarehouse_No.Text,
                    LockWarehouse_Date = DateTime.Now,
                    LockWarehouse_Note = txtLockWarehouse_Note.Text,
                    LockWarehouse_Test = txtLockWarehouse_Test.Text,
                    LockWarehouse_User_Add = int.Parse(Session["Users_Id"].ToString()),
                    LockWarehouse_Date_Add = DateTime.Now,
                    LockWarehouse_Computer_Add = Environment.MachineName
                };
                var insert = LockWarehouse_BLL.Inserttb_LockWarehouse(obj);
                if (insert)
                {
                    foreach (DataRow rows in dtLockWarehouse_Detail.Rows)
                    {
                        var LockWarehouse_Detail = new LockWarehouse_Detail_ET
                        {
                            LockWarehouse_ID = obj.LockWarehouse_ID,
                            Warehouse_ID = int.Parse(rows["Warehouse_Id"].ToString()),
                            LockWarehouse_Detail_TD = int.Parse(rows["LockWarehouse_Detail_TD"].ToString()),
                            LockWarehouse_Detail_NTK = int.Parse(rows["LockWarehouse_Detail_NTK"].ToString()),
                            LockWarehouse_Detail_XTK = int.Parse(rows["LockWarehouse_Detail_XTK"].ToString()),
                            LockWarehouse_Detail_TC = int.Parse(rows["LockWarehouse_Detail_TC"].ToString()),
                            LockWarehouse_Detail_Test = int.Parse(rows["LockWarehouse_Detail_Test"].ToString()),
                            LockWarehouse_Detail_TestName = rows["LockWarehouse_Detail_TestName"].ToString() == string.Empty ? txtLockWarehouse_Test.Text : rows["Test"].ToString(),
                        };
                        LockWarehouse_Detail_BLL.Inserttb_LockWarehouse_Detail(LockWarehouse_Detail);
                    }
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    grLockWarehouse_Detail.GetStore().RemoveAll();
                }
                GetData();
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #region[ClearTextbox]
        private void ClearTextBox()
        {
            FormPanelLockWarehouse.Reset();
            txtLockWarehouse_No.Text = MaTuDong();
        }
        #endregion
        #region[MaTangTuDong]
        protected string MaTuDong()
        {
            int i;
            DataTable dt = clsLockWarehouse.BindGirdtb_LockWarehouse(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["LockWarehouse_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["LockWarehouse_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["LockWarehouse_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "CK_00" + i;
        }
        #endregion

        protected void UnLock_Click(object sender, DirectEventArgs e)
        {
            var sm = grLockWarehouse.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    try
                    {
                        LockWarehouse_BLL.Deletetb_LockWarehouse(int.Parse(row.RecordID));
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetData();
        }
    }
}